package com.xr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xr.mapper.MemberMapper;
import com.xr.pojo.CheckGroup;
import com.xr.pojo.Member;
import com.xr.pojo.MemberExample;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.service.MemberService;
import com.xr.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    //根据手机号查询会员info
    @Override
    public Member queryByTelephone(String telephone) {
        MemberExample me = new MemberExample();
        MemberExample.Criteria mec = me.createCriteria();
        mec.andPhonenumberEqualTo(telephone);
        List<Member> members = memberMapper.selectByExample(me);
        return  members.get(0);
    }
    //添加
    @Override
    public void insertMember(Member member) {
        memberMapper.insertSelective(member);
    }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(),true);
        Page<Member> list = memberMapper.selectByQueryString(queryPageBean.getQueryString());
        return new PageResult(list.getTotal(), list.getResult());
    }

    //根据id查询会员info
    @Override
    public Member findById(Integer id) {
        return memberMapper.selectByPrimaryKey(id);
    }

    //修改
    @Override
    public void update(Member member) {
        memberMapper.updateByPrimaryKey(member);
    }

    //删除
    @Override
    public void delete(Integer id) {
        memberMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Integer> getMemberReport(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        months.forEach(month -> {
            Integer count = memberMapper.findMemberCountBeforeDate(month+".01");
            memberCount.add(count);
        });
        return memberCount;
    }

    @Override
    public List<Integer> getMemberDateSum(List<String> months) {
        List<Integer> memberDateSums=new ArrayList<>();
        months.forEach(s -> {
            System.out.println(s);
            Integer memberDateSum = memberMapper.findMemberCountBeforeDate(s+"_01");
            memberDateSums.add(memberDateSum);
            System.out.println(memberDateSum);
        });
        return memberDateSums;
    }
}
